<template>
  <div class="login-page">
  <el-card>
     <template #header>黑马面经运营后台</template>
    <el-form :model="formData" :rules="formDataRules"  ref="formDataRef">
      <el-form-item label="用户名:" prop="username">
        <el-input  v-model="formData.username " placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item    label="密码:" prop="password">
        <el-input v-model="formData.password "  placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="doLogin" >登录</el-button>
        <el-button @click="doReset" >重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
 </div>
</template>

<script>
// import { userLogin } from '@/api/user.js'
// 辅助函数写法
import { mapActions, mapMutations } from 'vuex'
export default {
  name: 'login-page',
  data () {
    return {
      formData: {
        username: '',
        password: ''
      },
      formDataRules: {
        username: [{ required: true, message: '用户名不得为空！', trigger: 'blur' },
          { pattern: /^\w{5,15}$/, message: '用户名必须5-15位!', trigger: 'blur' }],

        password: [
          { required: true, message: '密码不得为空！', trigger: 'blur' },
          { pattern: /^\w{5,15}$/, message: '密码必须5-15位!', trigger: 'blur' }]
      }
    }
  },
  methods: {
    ...mapActions('user', ['getToken']),
    ...mapMutations('user', ['updateToken']),
    doLogin () {
      // (1) 验证是否正确
      this.$refs.formDataRef.validate(async valid => {
        if (valid) {
          // (2) 发送ajax
        /*   const { data } = await userLogin(this.formData)
          console.log(data) */

          await this.getToken(this.formData)
          // (3) 跳转首页
          this.$router.push('/')
        }
      })
    },
    // 重置
    doReset () {
      this.$refs.formDataRef.resetFields()
    }
  }
}

</script>

<style lang="scss" scoped>
  /*
  1 deep深度选择器的作用: 让父组件 可以选中文件中内部的盒子
  2. 语法:
  scss语言 ::v-deep
  less语言  /deep/
  3.场景  需要在父组件中选中子组件内部的盒子
  */
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
::v-deep .el-card__header{
  height: 80px;
  line-height: 40px;
  text-align: center;
  background-color: #727cf5;
  font-size: 22px;
  color: #fff;
}
  }
  .el-form {
    padding: 0 20px;
    .el-input-inner{
      outline-color:green
    }
  }
  .tc {
    text-align: center;
  }

}
</style>
